package com.netease.nie.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.netease.nie.base.controllers.BaseController;
import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.base.protocol.SystemResponseCode;
import com.netease.nie.dto.TopicDetailDto;
import com.netease.nie.dto.TopicSnapshotDto;
import com.netease.nie.facade.TopicFacade;

@RestController
@RequestMapping("/api/topic")
public class TopicController extends BaseController {
	
	@Autowired
	private TopicFacade topicFacade;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	public String topicList(Integer page) throws Exception {
		if (page == null)
			page = 1;
		List<TopicSnapshotDto> topicSnapshotDtos = topicFacade.queryTopicSnapshotDtos(page);
		return jsonSuccess(topicSnapshotDtos);
	}
	
	@RequestMapping(value = "/detail", method = RequestMethod.GET, produces = "application/json")
	public String topicDetail(@RequestParam(name = "topic_id", required = false) Integer topicId) throws Exception {
		if (topicId == null)
			throw new BusinessException(SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(), "主题id为空");
		
		TopicDetailDto topicDetailDto = topicFacade.queryTopicDetailDtoByTopicId(topicId);
		return jsonSuccess(topicDetailDto);
	}
}
